<template>
  <div>
    <h1>商品类型</h1>
    <div>
      <el-button type="primary" @click="handleAdd">添加</el-button>
    </div>
  </div>
  <el-table
    :data="typeTree"
    style="width: 100%; margin-bottom: 20px"
    row-key="id"
    border
    default-expand-all
  >
    <el-table-column prop="name" label="名称" sortable />
    <el-table-column prop="typeStatus" label="状态" sortable>
      <template #default="scope">
        <div style="display: flex; align-items: center">
          <el-tag v-if="scope.row.typeStatus == 0" type="success">显示</el-tag>
          <el-tag v-if="scope.row.typeStatus == 1" type="danger">隐藏</el-tag>
        </div>
      </template>
    </el-table-column>
    <el-table-column prop="typeTop" label="排序" sortable />
    <el-table-column prop="createTime" label="创建时间" sortable />
    <el-table-column label="类型图片" sortable>
      <template #default="scope">
        <el-image
          v-if="scope.row.typeImage != null"
          :src="scope.row.typeImage"
          style="width: 50px; height: 50px"
        ></el-image>
      </template>
    </el-table-column>

    <el-table-column label="操作" sortable>
      <template #default="scope">
        <el-button size="small" @click="handleEdit(scope.row)">
          编辑
        </el-button>
        <el-button size="small" type="danger" @click="handleDelete(scope.row)">
          删除
        </el-button>
        <el-button
          size="small"
          v-if="scope.row.typeStatus == 1"
          type="success"
          @click="handleChange(scope.row, 0)"
        >
          显示
        </el-button>
        <el-button
          v-if="scope.row.typeStatus == 0"
          size="small"
          type="primary"
          @click="handleChange(scope.row, 1)"
        >
          隐藏
        </el-button>
        <el-button size="small" type="success" @click="onAddType(scope.row)">
          添加
        </el-button>
      </template>
    </el-table-column>
  </el-table>
  <el-dialog v-model="dialogFormVisible" :title="title" width="500">
    <el-form :model="form">
      <el-form-item label="分类名称" :label-width="formLabelWidth">
        <el-input v-model="form.typeName" autocomplete="off" />
      </el-form-item>
      <el-form-item label="分类图片" :label-width="formLabelWidth">
        <el-upload
          class="avatar-uploader"
          action="http://localhost:10010/shop/type/uploadFile"
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :headers="headers"
        >
          <img v-if="imageUrl" :src="imageUrl" class="avatar" />
          <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
        </el-upload>
      </el-form-item>
      <el-form-item label="分类排序" :label-width="formLabelWidth">
        <el-input v-model="form.typeTop" autocomplete="off" />
      </el-form-item>
      <el-form-item label="状态" :label-width="formLabelWidth">
        <el-radio-group v-model="form.typeStatus">
          <el-radio :value="0">显示</el-radio>
          <el-radio :value="1">隐藏</el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="dialogFormVisible = false">放弃</el-button>
        <el-button
          type="primary"
          v-if="title == '添加分类'"
          @click="onSubmitAddType"
        >
          添加
        </el-button>
        <el-button
          type="primary"
          v-if="title != '添加分类'"
          @click="onSubmitEditType"
        >
          修改
        </el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import { onMounted, ref } from "vue";
import {
  getTypeTree,
  setTypeStatus,
  deleteTypeById,
  addType,
  updateType,
} from "@/api/api";
import { ElMessage, ElMessageBox } from "element-plus";
const typeTree = ref([]);
const dialogFormVisible = ref(false);
const formLabelWidth = "140px";
import { Plus } from "@element-plus/icons-vue";
import type { UploadProps } from "element-plus";
import { load } from "@amap/amap-jsapi-loader";
const title = ref("添加类型");

const imageUrl = ref("");
const loadTypeTree = () => {
  getTypeTree().then((res) => {
    if (res.data.code == 200) {
      typeTree.value = res.data.data;
    }
  });
};
const form = ref({
  typeId: null,
  typePid: 0,
  typeName: null,
  typeStatus: 0,
  typeTop: null,
  typeImage: null,
});
const onAddType = (row: any) => {
  console.log(row);
  title.value = "添加分类";
  form.value.typePid = row.id;
  dialogFormVisible.value = true;
};
const headers = {
  token: sessionStorage.getItem("token"),
};
const handleAdd = () => {
  title.value = "添加分类";
  form.value.typePid = 0;
  dialogFormVisible.value = true;
};

const handleChange = (row: any, status: any) => {
  setTypeStatus(row.id, status).then((res) => {
    if (res.data.code == 200) {
      ElMessage.success("修改成功");
      loadTypeTree();
    } else {
      ElMessage.error(res.data.msg);
    }
  });
};
const onSubmitEditType = () => {
  form.value.typeImage = imageUrl.value;
  updateType(form.value).then((res) => {
    if (res.data.code == 200) {
      ElMessage.success("修改成功");
      loadTypeTree();
      dialogFormVisible.value = false;
    } else {
      ElMessage.error(res.data.msg);
    }
  });
};
const onSubmitAddType = () => {
  addType(form.value).then((res) => {
    if (res.data.code == 200) {
      ElMessage.success("添加成功");
      loadTypeTree();
      dialogFormVisible.value = false;
    } else {
      ElMessage.error(res.data.msg);
    }
  });
};
const handleAvatarSuccess: UploadProps["onSuccess"] = (
  response,
  uploadFile
) => {
  imageUrl.value = response.data;
  form.value.typeImage = imageUrl.value;
  console.log("handleAvatarSuccess", imageUrl.value);
};

const handleEdit = (row: any) => {
  console.log(row);
  title.value = "编辑类型";
  Object.assign(form.value, row);
  form.value.typeName = row.name;
  form.value.typeId = row.id;
  form.value.typePid = row.parentId;
  imageUrl.value = row.typeImage;
  dialogFormVisible.value = true;
};
const handleDelete = (row: any) => {
  console.log(row);
  ElMessageBox.confirm("删除数据之后，无法恢复，您确认要删除吗?", "警告", {
    confirmButtonText: "OK",
    cancelButtonText: "Cancel",
    type: "warning",
  })
    .then(() => {
      deleteTypeById(row.id).then((res) => {
        if (res.data.code == 200) {
          ElMessage.success("删除成功");
          loadTypeTree();
        } else {
          ElMessage.error(res.data.msg);
        }
      });
    })
    .catch(() => {
      ElMessage({
        type: "info",
        message: "Delete canceled",
      });
    });
};
onMounted(() => {
  loadTypeTree();
});
</script>
<style scoped>
.avatar-uploader .avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed var(--el-border-color);
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
  border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  text-align: center;
}
</style>
